home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM3.lzh / AmigaDOS / Example1.c < prev    next >
C/C++ Source or Header  |  1991-01-13  |  2KB  |  94 lines

  1. /* Example1                                                             */
  2. /* This program collects ten integer values from the user, and saves    */
  3. /* them in a file ("HighScore.dat") on the RAM disk. The memory is then */
  4. /* cleared, and the file cursor is moved to the beginning of the file.  */
  5. /* The file is then loaded into the memory again, and printed out.      */
  6.  
  7.  
  8.  
  9. #include <libraries/dos.h>
  10.  
  11.  
  12. void main();
  13.  
  14. void main()
  15. {
  16.   struct FileHandle *file_handle;
  17.   int highscore[ 10 ];
  18.   long bytes_written;
  19.   long bytes_read;
  20.   int loop;  
  21.  
  22.  
  23.  
  24.   /* Let the user enter ten integer values: */
  25.   for( loop=0; loop < 10; loop++ )
  26.   {
  27.     printf("Highscore[%d]: ", loop );
  28.     scanf("%d", &highscore[ loop ] );
  29.   }
  30.  
  31.  
  32.  
  33.   /* Try to open file "HighScore.dat" as a new file:  */
  34.   /* (If the file does not exist, it will be created. */
  35.   /* If it, on the the other hand, exist, it will be  */
  36.   /* overwritten.)                                    */
  37.   file_handle = (struct FileHandle *)
  38.     Open( "RAM:HighScore.dat", MODE_NEWFILE );
  39.   
  40.   /* Have we opened the file successfully? */
  41.   if( file_handle == NULL )
  42.   {
  43.     printf("Could not open the file!\n");
  44.     exit();
  45.   }
  46.  
  47.  
  48.  
  49.   /* We have now opened a file, and are ready to start writing: */
  50.   bytes_written = Write( file_handle, highscore, sizeof( highscore ) );
  51.  
  52.   if( bytes_written != sizeof( highscore ) )
  53.   {
  54.     printf("Could not save the Highscore list!\n");
  55.     Close( file_handle );
  56.     exit();
  57.   }
  58.   else
  59.     printf("Highscore saved successfully!\n");
  60.  
  61.  
  62.  
  63.   printf("Memory cleared!\n");
  64.   
  65.   for( loop=0; loop < 10; loop++ )
  66.     highscore[ loop ] = 0;  
  67.  
  68.  
  69.  
  70.   printf("Loading Highscore!\n");
  71.  
  72.   Seek( file_handle, 0, OFFSET_BEGINNING );
  73.  
  74.   bytes_read = Read( file_handle, highscore, sizeof( highscore ) );
  75.  
  76.   if( bytes_written != sizeof( highscore ) )
  77.   {
  78.     printf("Could not read the Highscore list!\n");
  79.     Close( file_handle );
  80.     exit();
  81.   }
  82.  
  83.  
  84.  
  85.   /* Print out the numbers: */
  86.   for( loop=0; loop < 10; loop++ )
  87.     printf("Highscore[%d] = %5d\n", loop, highscore[ loop ] );
  88.  
  89.  
  90.  
  91.   /* Close the file: */
  92.   Close( file_handle );
  93. }
  94.